package com.mashibing.custompredicate;

import lombok.Getter;
import lombok.Setter;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.util.Collections;
import java.util.List;
import java.util.function.Predicate;
@Component
public class MyVersionRoutePredicateFactory extends AbstractRoutePredicateFactory<MyVersionRoutePredicateFactory.MyConfig> {


    public MyVersionRoutePredicateFactory() {
        super(MyConfig.class);
    }

    @Override
    public List<String> shortcutFieldOrder() {
        return Collections.singletonList("version");
    }

    @Override
    public Predicate<ServerWebExchange> apply(MyConfig config) {
        return (serverWebExchange) -> {
            String version = serverWebExchange.getRequest().getQueryParams().getFirst("version");
            return version != null && version.equals(config.getVersion());
        } ;
    }

    @Setter
    @Getter
    public static class MyConfig {
        private String version;

    }
}
